
function dateFormat(timestamp, format) {
	// formats格式
	// 1. Y-M-D
	// 2. Y-M-D h:m:s
	// 3. Y年M月D日
	// 4. Y年M月D日 h时m分
	format = format || 'Y-M-D';
	
	console.log(timestamp);
	let zero = function(value) {
		if (value < 10) {
			return "0" + value;
		}
		return value;
	}

	let myDate = timestamp ? new Date(timestamp) : new Date();
	
	console.log(myDate);
	let year = myDate.getFullYear();
	let month = zero(myDate.getMonth() + 1);
	let day = zero(myDate.getDate());

	let hour = zero(myDate.getHours());
	let minite = zero(myDate.getMinutes());
	let second = zero(myDate.getSeconds());

		return format.replace(/Y|M|D|h|m|s/g, function(matches) {
			return ({
				Y: year,
				M: month,
				D: day,
				h: hour,
				m: minite,
				s: second
			})[matches]
		});
}


function checkLogin() {
	let token = uni.getStorageSync('token')
	if(!token){
		return true
	}else{
		return false
	}
}


function checkPhone(value) {
	var myreg=/^[0-9|\+]*$/
	if (!myreg.test(value)) {
		return false;
	} else {
		return true;
	}
}

function checkCNPhone(value) {
	var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if (!myreg.test(value)) {
		return false;
	} else {
		return true;
	}
}


function getCurrentMonthDay(year, month) {
  var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
  var startDay = '01'
  var endDay = null
  switch (month) {
    case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
      endDay = 31
      break;
    case '04':
    case '06':
    case '09':
    case '11':
      endDay = 30
      break;
    case '02':
      endDay = flag ? 29 : 28
      break;
    default:
      endDay = '月份格式不正确，请重新输入！'
  }
  return {
	  startTime: year+'-'+month+'-'+startDay,
	  endTime: year+'-'+month+'-'+endDay
  }
}

function checkReport(arr) {
	for(let i=0; i<arr.length; i++){
		for(let j=i+1; j<arr.length; j++){
			if(arr[i].id == arr[j].id){
				arr.splice(j,1)
				j--
			}
		}
	}
	return arr
}

function checkNumber(val) {
　　if (parseFloat(val).toString() == "NaN") {
　　　　return false;
　　} else {
　　　　return true;
　　}

}
let constants = {
	 fromList : {
	  title: '选择时间',
	  custom: true,
	  fromTxt: [
	    { text: '全部', val: '' },
	    { text: '今天', val: 'today' },
	    { text: '昨天', val: 'yesterday' },
	    { text: '最近7天', val: 'lately7' },
	    { text: '最近30天', val: 'lately30' },
	    { text: '本月', val: 'month' },
	    { text: '本年', val: 'year' }
	  ] 
	},
	 birRecentList : {
	  title: '选择时间',
	  custom: true,
	  fromTxt: [
	    { text: '今天', val: 'today' },
	    { text: '最近7天', val: 'next7' },
	    { text: '本月', val: 'month' },
	    { text: '下月', val: 'nextMonth' }
	  ]
	},
	 achieveRecentList : {
	  title: '选择时间',
	  custom: true,
	  fromTxt: [
	    { text: '今天', val: 'today' },
	    { text: '昨天', val: 'yesterday' },
	    { text: '最近7天', val: 'lately7' },
	    { text: '本月', val: 'month' },
	    { text: '上月', val: 'preMonth' }
	  ]
	}
}

module.exports = {
	checkLogin:checkLogin,
	checkNumber:checkNumber,
	dateFormat,
	checkPhone:checkPhone,
	checkCNPhone:checkCNPhone,
	checkReport:checkReport,
	getCurrentMonthDay:getCurrentMonthDay,
	constants:constants
}